﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BTGISWASECO.DataLayer;
using BTGISWASECO.BusinessEntities;
using BTGISWASECO.BusinessLayer;

public partial class PresentationLayer_DefaultTab : System.Web.UI.Page
{
    VanBLL bll = new VanBLL();
    DMHuongDongChayDAL DMHuongDongChayDal = new DMHuongDongChayDAL();
    VanEntities VanEntities = new VanEntities();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["DataTable"] == "")
                Response.Redirect("../default.aspx");
            for (int i = DateTime.Now.Year; i >= 1990; --i)
            {
                ListItem iteml = new ListItem(i.ToString(), i.ToString());
                DListNamLD.Items.Add(iteml);
            }
            DListNamLD.SelectedValue = VanEntities.NamLapDat.ToString();
            VanEntities = bll.GetVanEntitiesByID();
            txtObjectID.Value = VanEntities.OBJECTID.ToString();
            txtIDVan.Text = VanEntities.IDVan;
            this.txtDMA.Text = VanEntities.IDDMA;
            this.txtDMZ.Text = VanEntities.IDDMZ;
            foreach (var itemc in DMHieuVanDAL.GetList())
            {
                if (VanEntities.NhanHieuText.Trim() == itemc.MoTa.ToString().Trim())
                    VanEntities.NhanHieu = itemc.Ma;
                ListItem iteml = new ListItem(itemc.MoTa.ToString(), itemc.Ma.ToString());
                DListNhanHieu.Items.Add(iteml);
            }

            DListNhanHieu.SelectedValue = VanEntities.NhanHieu.ToString();
            // this.DListHDChay.DataSource = DMHuongDongChayDal.GetDMHuongDongChayEntitiesByID();
            this.txtSoNha.Text = VanEntities.SoNha.ToString();
            this.txtMaDuong.Text = VanEntities.MaDuong.ToString();
            this.txtMaPhuong.Text = VanEntities.MaPhuong.ToString();
            this.txtMaQuan.Text = VanEntities.MaQuan;
            this.txtCoChiaKhoa.Text = VanEntities.CoChiaKhoa.ToString();
            this.txtNgayCNC.Text = VanEntities.NgayCNCuoi.ToString("dd/MM/yyyy");
            this.txtMoTaViTri.Text = VanEntities.MoTaViTri.ToString();
            this.dDoSau.Text = VanEntities.DoSau.ToString();
            this.txtNguoiCNC.Text = VanEntities.NguoiCNCuoi.ToString();
            this.txtMaHinhAnh.Text = VanEntities.IDHinhAnh;
            this.txtGhiChu.Text = VanEntities.GhiChu;
            this.nSVDongVan.Text = VanEntities.SVDongVan.ToString();
            this.nSLKPSuCo.Text = VanEntities.SoLanSuCo.ToString();
            //DateTime dob = DateTime.Parse(Request.Form[txtNgayCNC.UniqueID]); DMLoaiDonVi
            foreach (var itemk in DMChieuDongVanDAL.GetList())
            {
                if (VanEntities.ChieuDongVanText.ToString().Trim() == itemk.MoTa.ToString().Trim())
                    VanEntities.ChieuDongVan = itemk.Ma;
                ListItem iteml = new ListItem(itemk.MoTa.ToString(), itemk.Ma.ToString());
                DListChieuDongVan.Items.Add(iteml);
            }

            DListChieuDongVan.SelectedValue = VanEntities.ChieuDongVan.ToString();

            foreach (var itemvl in DMVatLieuDAL.GetList())
            {
                if (VanEntities.VatLieuText.ToString().Trim() == itemvl.MoTa.ToString().Trim())
                    VanEntities.VatLieu = itemvl.Ma;
                ListItem iteml = new ListItem(itemvl.MoTa.ToString(), itemvl.Ma.ToString());
                DListVatLieu.Items.Add(iteml);
            }
            DListVatLieu.SelectedValue = VanEntities.VatLieu.ToString();

            foreach (var itemtc in DMTieuChuanVanDAL.GetList())
            {
                if (VanEntities.TieuChuanVanText.ToString().Trim() == itemtc.MoTa.ToString().Trim())
                    VanEntities.TieuChuanVan = itemtc.Ma;
                ListItem iteml = new ListItem(itemtc.MoTa.ToString(), itemtc.Ma.ToString());
                DListTieuChuan.Items.Add(iteml);
            }
            DListTieuChuan.SelectedValue = VanEntities.TieuChuanVan.ToString();

            foreach (var itemnsx in DMNhaSanXuatDAL.GetList())
            {
                if (VanEntities.NhaSanXuatText.ToString().Trim() == itemnsx.MoTa.ToString().Trim())
                    VanEntities.NhaSanXuat = itemnsx.Ma;
                ListItem iteml = new ListItem(itemnsx.MoTa.ToString(), itemnsx.Ma.ToString());
                DListNhaSX.Items.Add(iteml);
            }
            DListNhaSX.SelectedValue = VanEntities.NhaSanXuat.ToString();

            foreach (var itemnusx in DMNuocSanXuatDAL.GetList())
            {
                if (VanEntities.NuocSanXuatText.ToString().Trim() == itemnusx.MoTa.ToString().Trim())
                    VanEntities.NuocSanXuat = itemnusx.Ma;
                ListItem iteml = new ListItem(itemnusx.MoTa.ToString(), itemnusx.Ma.ToString());
                DListNuocSX.Items.Add(iteml);
            }
            DListNuocSX.SelectedValue = VanEntities.NuocSanXuat.ToString();

            foreach (var itemnttsd in DMTinhTrangSDDAL.GetList())
            {
                if (VanEntities.TinhTrangText.ToString().Trim() == itemnttsd.MoTa.ToString().Trim())
                    VanEntities.TinhTrang = itemnttsd.Ma;
                ListItem iteml = new ListItem(itemnttsd.MoTa.ToString(), itemnttsd.Ma.ToString());
                DListTTSD.Items.Add(iteml);
            }
            DListTTSD.SelectedValue = VanEntities.TinhTrang.ToString();

            foreach (var itemntldv in DMLoaiDonViDAL.GetList())
            {
                if (VanEntities.DonViQLText.ToString().Trim() == itemntldv.MoTa.ToString().Trim())
                    VanEntities.DonViQL = itemntldv.Ma;
                ListItem iteml = new ListItem(itemntldv.MoTa.ToString(), itemntldv.Ma.ToString());
                DListDonViQL.Items.Add(iteml);
            }
            DListDonViQL.SelectedValue = VanEntities.DonViQL.ToString();

            foreach (var itemkcv in DMKichCoVanDAL.GetList())
            {
                if (VanEntities.CoVanText.ToString().Trim() == itemkcv.MoTa.ToString().Trim())
                    VanEntities.CoVan = itemkcv.Ma;
                ListItem iteml = new ListItem(itemkcv.MoTa.ToString(), itemkcv.Ma.ToString());
                DListKichCoVan.Items.Add(iteml);
            }
            DListKichCoVan.SelectedValue = VanEntities.CoVan.ToString();

            foreach (var itemlv in DMLoaiVanDAL.GetList())
            {
                if (VanEntities.LoaiVanText.ToString().Trim() == itemlv.MoTa.ToString().Trim())
                    VanEntities.LoaiVan = itemlv.Ma;
                ListItem iteml = new ListItem(itemlv.MoTa.ToString(), itemlv.Ma.ToString());
                DListLoaiVan.Items.Add(iteml);
            }
            if(VanEntities.LoaiVan!=null)
            DListLoaiVan.SelectedValue = VanEntities.LoaiVan.ToString();

            foreach (var itemcnv in DMChucNangVanDAL.GetList())
            {
                if (VanEntities.ChucNangVanText.ToString().Trim() == itemcnv.MoTa.ToString().Trim())
                    VanEntities.ChucNangVan = itemcnv.Ma;
                ListItem iteml = new ListItem(itemcnv.MoTa.ToString(), itemcnv.Ma.ToString());
                DListChucNangVan.Items.Add(iteml);
            }
            DListChucNangVan.SelectedValue = VanEntities.ChucNangVan.ToString();

            foreach (var itemtsh in DMThuocSoHuuDAL.GetList())
            {
                if (VanEntities.ThuocSoHuuText.ToString().Trim() == itemtsh.MoTa.ToString().Trim())
                    VanEntities.ThuocSoHuu = itemtsh.Ma;
                ListItem iteml = new ListItem(itemtsh.MoTa.ToString(), itemtsh.Ma.ToString());
                DListThuocSoHuu.Items.Add(iteml);
            }
            DListThuocSoHuu.SelectedValue = VanEntities.ThuocSoHuu.ToString();

        }//ThuocSoHuu
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        VanEntities bk = new VanEntities();
        VanDAL VanDAL = new VanDAL();
        //VanEntities VanEntities = new VanEntities(Convert.ToInt32(DListDonViTC.SelectedValue = DListDonViTC.SelectedValue!=""?DListDonViTC.SelectedValue:"0"),
        //    Convert.ToInt32(DListDonViGS.SelectedValue = DListDonViGS.SelectedValue!=""?DListDonViGS.SelectedValue:"0"), this.txtNguoiCNC.Text, DateTime.Parse(this.txtNgayCNC.Text.ToString()), this.txtGhiChu.Text, this.txtMaHinhAnh.Text);
        VanEntities VanEntities = new VanEntities(Convert.ToInt32(txtObjectID.Value), this.txtIDVan.Text,this.txtDMZ.Text, this.txtDMA.Text,
           Convert.ToInt32(DListVatLieu.SelectedValue = DListVatLieu.SelectedValue != "" ? DListVatLieu.SelectedValue : "0"), 
           Convert.ToInt32(DListTieuChuan.SelectedValue = DListTieuChuan.SelectedValue != "" ? DListTieuChuan.SelectedValue : "0"), 
           Convert.ToInt32(DListNhanHieu.SelectedValue = DListNhanHieu.SelectedValue != "" ? DListNhanHieu.SelectedValue : "0"), 
           Convert.ToInt32(DListNhaSX.SelectedValue = DListNhaSX.SelectedValue != "" ? DListNhaSX.SelectedValue : "0"), 
           Convert.ToInt32(DListNuocSX.SelectedValue = DListNuocSX.SelectedValue != "" ? DListNuocSX.SelectedValue : "0"), 
           Convert.ToInt32(DListNamLD.SelectedValue),Convert.ToDouble(this.dDoSau.Text),
           Convert.ToInt32(DListChieuDongVan.SelectedValue = DListChieuDongVan.SelectedValue != "" ? DListChieuDongVan.SelectedValue : "0"),
           Convert.ToDouble(this.nSVDongVan.Text),this.txtMoTaViTri.Text, Convert.ToInt32(DListTTSD.SelectedValue = DListTTSD.SelectedValue != "" ? DListTTSD.SelectedValue : "0"),
           Convert.ToInt32(DListDonViQL.SelectedValue = DListDonViQL.SelectedValue != "" ? DListDonViQL.SelectedValue : "0"),
           this.txtCoChiaKhoa.Text,this.txtSoNha.Text, this.txtMaDuong.Text, this.txtMaPhuong.Text,this.txtMaQuan.Text,
           Convert.ToInt32(DListKichCoVan.SelectedValue = DListKichCoVan.SelectedValue != "" ? DListKichCoVan.SelectedValue : "0"),
           DListLoaiVan.SelectedValue = DListLoaiVan.SelectedValue != "" ? DListLoaiVan.SelectedValue : "", 
           Convert.ToInt32(DListChucNangVan.SelectedValue = DListChucNangVan.SelectedValue != "" ? DListChucNangVan.SelectedValue : "0"), 
           Convert.ToInt32(DListThuocSoHuu.SelectedValue = DListThuocSoHuu.SelectedValue != "" ? DListThuocSoHuu.SelectedValue : "0"),
           Convert.ToInt32(this.nSLKPSuCo.Text), DateTime.Parse(Request.Form.Get("txtNgayCNC")), this.txtNguoiCNC.Text, this.txtMaHinhAnh.Text, this.txtGhiChu.Text);
        VanDAL.Update1(VanEntities);
        //VanDAL.Update(Convert.ToInt32(txtObjectID.Value), txtMaDoanOng.Text, txtDMA.Text, Convert.ToInt32(DListHDChay.SelectedValue), Convert.ToDouble(this.nChieuDai.Text));
      //  Response.Redirect("../default.aspx");
        ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "<script language='JavaScript'>alert('Cập nhật thành công'); window.location.href ='../Default.aspx';</script>");
    }
    protected void btnHuyBo_Click(object sender, EventArgs e)
    {
        Response.Redirect("../default.aspx");
    }
}